// pages/classification/classification.js
var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  // md5 = require('../../utils/md5.js'),
  app = getApp(),
   to,
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commodityList: [],
    cateindex: -2,
    pagesize: 6,
    start: 0,
    cid: 0,
    hideModal:!0,
    btnIndex: -2,
    show: false,
    src: '../pic/up.png',
    goods: [],
    allLabels:[],
    promotionprice:'',
    headimg,
    list:[],
    count: 1,
    btmimg:'https://shandongweimiao.cn/api/imgs/2020-06-01/e386bf910b0b4d90be267bc731ed53e2.png',
    actEndTimeList:[],
    countDownList: [],
    todayup:[],
    classify:[],
    showmodal:0,
    id:"",
    buysrc:'',
    buyprice:'',
    buystore:'',
    goodsnum:'',
    speccont:'',
    indexsel:0,
    goodsdata:{},
    membernum:0,
    // showchose: !1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
        if (app.logininfo) {
          that.getdata()
        } else {
          util.setwatcher(app, this.afterlogin)
        }
      // wx.showLoading({
      //   title: '加载中',
      // })
  },
  afterlogin: {
    token: res => {
      that.getdata();
      // that.gettodaylist();
      // that.getclassifylist();

    //  that.getCoupon();
    }
  },
//搜索商品
  searchgoods(){
    wx.navigateTo({
      url: '../searchgoods/searchgoods?page=1',
    })
  },
  //获取分类函数
  getclassifylist(){
  },

  //获取今日上新
  gettodaylist(){
    let { todayup, pagesize, start, } = that.data
    api.todaynewlist({start,pagesize},res=>{
      if (res.data.issuccess == 1) {
        that.setData({todayup: res.data.list })

      }
    })
  },
  jumptogoodsdetail(e){
    let { id } = e.currentTarget.dataset;
    console.log(e, 'idid')
    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?id=' + id,
    })
  },
  //今日上新详情
  // todetail(e){

  //   let {id}=e.currentTarget.dataset
  //   wx.navigateTo({
  //     url: '../goodsDetail/goodsDetail?id='+id,
  //   })
  // },

//获取更多商品

  getmore(e){
    let {id,mainpicpath} = e.currentTarget.dataset
    wx.navigateTo({
      url: '../list/list?id='+id+'&mainpicpath='+mainpicpath,
    })
   },


//显示购物车弹窗
   //显示购物车弹窗
   showcarmodel(e){
    let {id,item} = e.currentTarget.dataset,{buysrc,buyprice,buystore,goodsnum,usertype,count} = that.data
    that.setData({
      buyprice: (item.tygprice/100).toFixed(2),
      price: (item.promotionprice/100).toFixed(2),
      totalscore:item.tygscore,
      score:item.tygscore,
      count:1,
      anim: 'sizewrap 0.3s',
      goodsdata:item
    })
    that.setData({
      showmodal:1,
    },()=>{      
   
    })
  },
  closechose() {
    that.setData({
      showmodal: !1,
      anim: ''
    })
  },

  //加入购物车
  addshopcar(){
    let { count,goodsdata }=that.data,paynum='',cartids=wx.getStorageSync('cartids')
    paynum=count
    let data={  paynum,type:'GOODS',goodsid:goodsdata.id}
      api.addcart(data,res=>{
        if(res.data.issuccess == 1){
          wx.showToast({
            title: '加入购物车成功',
            icon: 'none',
            duration: 2000,
            success(){
              cartids.push(goodsdata)
              app.badege(cartids)
              wx.setStorageSync("cartids",cartids)
            }
          })
          that.setData({
            showmodal:0
          })
        }else{
          wx.showToast({
            title: '加入购物车失败',
            icon: 'none',
            duration: 2000
          })
        }
        })
    // }
  },
    // 选择规格
    specitemsel(e){
      let {index,speccont}=e.currentTarget.dataset
      that.setData({indexsel:index,speccont})
    },

  //关闭购物车弹窗
  closemodle(){
    that.setData({
      showmodal: 0,
    })
  },

  //购物车数量
  counttap(e) {
    let {
      count,buystore,goodsdata
    } = that.data
    if (e.currentTarget.dataset.code == 1) {
     if(count<goodsdata.kc){
       count++
     }
     else{
       wx.showToast({
         title: '商品库存不足',
         icon: 'none',
         duration: 2000
       })
     }
    } else {
      if (count > 1) {
        count--
      } else {
        wx.showToast({
          title: '至少添加一件商品',
          icon: 'none',
          duration: 2000
        })
      }
    }
    let buyprice =(Number(that.data.price)*count).toFixed(2)
    that.setData({
      buyprice,
      count
    })
 
  },
  toswiperd(e){
    let{path} = e.currentTarget.dataset;
    if(path!=null){
      wx.navigateTo({
        url: path,
      }
      )
    }
  },
  onPressCancel(){
    that.setData({hideModal:!0})
  },
  onPressComfirm(){
    that.setData({hideModal:!0})
    wx.navigateTo({
              url:"../member/member"
            })
  },
  getdata() {

    let { classify } = that.data, promotionprice = 0;
    api.getGoodsCateList( res => {
      wx.hideLoading()
      if (res.data.issuccess == 1) {
        console.log(res.data.list,'res.data.listres.data.list');
        classify = res.data.list
        classify.forEach(x=>{
          let list=[]
          x.list.forEach(y=>{
            y['newprice']=(y.promotionprice/100).toFixed(2)
            list.push(y)
          })
          //x['goodslist'] = list
        })
        that.setData({classify})
      }
    })
    api.getallswiper({type:'GOODS'},res=>{
      if(res.data.issuccess==1){
        that.setData({swipers:res.data.list},()=>{
          // that.getclassifylist();
        })
      }else{
        wx.hideLoading()
      }
    })
    // api.getallswiper({type:'LISTEN'},res=>{
    //   if(res.data.issuccess==1){
    //     console.log(res.data,'getallswipergetallswipergetallswiper')
    //     imglist=res.data.list
    //     that.setData({imglist})
    //   }
    // })
  },

  gotolist(e){
    let { id,name } = e.currentTarget.dataset;

    that.setData({catename:name},()=>{
      wx.navigateTo({
        url: '../list/list?id='+id,
      })
    })
  },
  toIntegralmall(){

    wx.navigateTo({
      url: '../scoregoods/scoregoods',
    })
  },
    togame(e){
        wx.navigateTo({
          url: '../game/game?id=3',
        })
      },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let cartids=wx.getStorageSync('cartids')
    app.badege(cartids);
    // that.setData({carlist})
    // api.getCartlist( res => {
    //   if (res.data.issuccess == 1) {
    //     app.badege(res.data.list)
    //     wx.setStorageSync("carlist", res.data.list)
    //   }
    // })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearTimeout(to)

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearTimeout(to)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // let { reachbtm } = this.data;

    // if (reachbtm) {
    //   return
    // }
    // that.setData({ start: that.data.start + that.data.pagesize }, () => {

    //   // that.getdata()
    //   that.getlist()
    // })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return{
      title: app.logininfo.appletname,
      path: `pages/listen/listen?share=`+app.logininfo.userid,

    }

  }
})